#!/bin/bash
# export JAVA_HOME="" # Set here if not set in environment
# export CUDA_PATH="" # Set here if not set in environment

MEMSIZE="-Xmx14G"
BIDMACH_ROOT="${BASH_SOURCE[0]}"
if [ ! `uname` = "Darwin" ]; then
  BIDMACH_ROOT=`readlink -f "${BIDMACH_ROOT}"`
else
  while [ -L "${BIDMACH_ROOT}" ]; do
    BIDMACH_ROOT=`readlink "${BIDMACH_ROOT}"`
  done
fi
BIDMACH_ROOT=`dirname "$BIDMACH_ROOT"`
pushd "${BIDMACH_ROOT}"  > /dev/null
BIDMACH_ROOT=`pwd -P`
BIDMACH_ROOT0="$( echo ${BIDMACH_ROOT} | sed 's+^/cygdrive/\(.\)+\1:+' )"
BIDMACH_ROOT="$( echo ${BIDMACH_ROOT0} | sed 's+^/mnt/\(.\)+\1:+' )"

LIBDIR="${BIDMACH_ROOT}/lib"

LOGGING_CONF="${BIDMACH_ROOT}/conf/logging.conf"
if [ -e "${LOGGING_CONF}" ]; then
    LOG_OPTS="-Djava.util.logging.config.file=${LOGGING_CONF}"
else
    LOG_OPTS=""
fi
export JAVA_OPTS="${MEMSIZE} -Xms128M -Dfile.encoding=UTF-8 ${LOG_OPTS} ${JAVA_OPTS}" # Set as much memory as possible

BIDMACH_VERSION=`echo target/BIDMach-*.jar | sed 's/.*BIDMach-//' | sed 's/\.jar//'`

OSS=`uname -a`
if [ "$ARCH" = "" ]; then
    ARCH=`arch`
fi
if [[ "$OSS" == Darwin* ]] ; then
    if [[ "$OSS" == *x86_64 ]] ; then
	    ARCH="x86_64"
    fi
    OSS="apple"
    JUPYTER_DIR="${HOME}/Library/Jupyter"
elif [[ "$OSS" == Linux* ]] ; then
    WSL=`cat /proc/version | grep 'Microsoft'`
    if [ ! "$WSL" = "" ]; then
	OSS="WSL"
    else
	OSS="linux"
    fi
    JUPYTER_DIR="${HOME}/.local/share/jupyter"
elif [[ "$OSS" == CYGWIN* ]] ; then
    if [[ "$OSS" == *WOW* ]] ; then
        ARCH="x86_64"
    fi
    OSS="windows"
    JUPYTER_DIR=`echo $APPDATA | sed 's_\\\\_/_g' | sed 's_^\([A-Z]\):_/cygdrive/\1_'`
    JUPYTER_DIR="${JUPYTER_DIR}/jupyter"
#    export TERM="cygwin"     # this should work for most cygwin setups. Try removing if it doesnt.
else
    echo "OS not supported" $OSS
    exit 1
fi

if [ "$OSS" = "windows" ]; then
    if [ ! "${JAVA_HOME}" = "" ]; then
        JAVA_HOME=`${BIDMACH_ROOT}/shortpath.bat "${JAVA_HOME}"`
        JAVA_HOME=`echo ${JAVA_HOME} | tr -d '\r'`
        export JAVA_HOME=`echo ${JAVA_HOME} | sed 's_\\\\_/_g'`/bin
    fi
fi

cd lib
BIDMACH_JARS=`echo *.jar`
BIDMACH_LIBS="${BIDMACH_ROOT}/target/BIDMach-${BIDMACH_VERSION}.jar"
for lib in ${BIDMACH_JARS}; do
    BIDMACH_LIBS="${BIDMACH_LIBS};${LIBDIR}/${lib}"
done

popd > /dev/null

TOOL_LIBS="${BIDMACH_ROOT}/conf;${JAVA_HOME}/lib/tools.jar"
ALL_LIBS="${TOOL_LIBS};${BIDMACH_LIBS}"

if [ "$OSS" = "windows" ]; then
    if [ ! "${CUDA_PATH}" = "" ]; then
	    NEWPATH=`${BIDMACH_ROOT}/shortpath.bat "${CUDA_PATH}"`
	    NEWPATH=`echo $NEWPATH | sed 's_\\\\_/_g'`/bin
    fi
else
    TOOL_LIBS=`echo "${TOOL_LIBS}" | sed 's/;/:/g'`
    ALL_LIBS=`echo "${ALL_LIBS}" | sed 's/;/:/g'`
fi

if [ "$1" = "notebook" ]; then
    shift 1
    KERNEL_CMD="[\"java\", \"-Xms128M\", \"${MEMSIZE}\", \"-Dfile.encoding=UTF-8\", \"-cp\", \"${ALL_LIBS}\",
                 \"-Dcoursier.mainJar=jupyter.scala.JupyterScala\", \"jupyter.scala.JupyterScala\", 
                 \"--id\", \"bidmach\", \"--name\", \"BIDMach\", \"--connection-file\", \"{connection_file}\"]"
    KERNEL_JSON="{\"language\" : \"bidmach\", \"display_name\" : \"BIDMach\", \"argv\" : ${KERNEL_CMD} }"
    mkdir -p "${JUPYTER_DIR}/kernels/bidmach"
    echo "${KERNEL_JSON}" > ${JUPYTER_DIR}/kernels/bidmach/kernel.json
    cp ${BIDMACH_ROOT}/logo-64x64.png ${JUPYTER_DIR}/kernels/bidmach
    jupyter notebook --KernelManager.kernel_cmd="${KERNEL_CMD}" $@
else
    userargs=""
    if [ "$2" != "" ]; then
	userargs="-Duser.arg0=$2"
	j=1;
	for i in ${*:3}; do
	    userargs="$userargs -Duser.arg$j=$i"
	    j=$((j+1))
	done
    fi
    runscript=""
    if [ "$1" != "" ]; then
	runscript="-I ${1}"
    fi
    ${BIDMACH_ROOT}/command/scala -Dscala.repl.maxprintstring=8000 ${userargs} -nobootcp -toolcp "${ALL_LIBS}" \
		   -cp "${ALL_LIBS}" -Yrepl-sync -I ${LIBDIR}/bidmach_init.sc $runscript
fi
